package cn.tataben.sharding.mappers;

import java.util.List;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;

import cn.tataben.sharding.domains.pos.UserPo;
import cn.tataben.sharding.mappers.provider.UserMapperProvider;

public interface UserMapper {

	@SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Integer.class)
	@InsertProvider(type = UserMapperProvider.class, method = "insert")
	int insert(UserPo user);

	@UpdateProvider(type = UserMapperProvider.class, method="update")
	int update(UserPo user);

	@Delete({ "delete from `user` where `id` = #{id, jdbcType = INTEGER}" })
	int delete(@Param("id") int id);

	@Select({
		"select `id`, `username`, `password` from `user` where `id` = #{id, jdbcType = INTEGER}"
	})
	@Results({
		@Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
		@Result(column = "username", property = "username", jdbcType = JdbcType.VARCHAR),
		@Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR)
	})
	UserPo select(@Param("id") int id);

	@SelectProvider(type = UserMapperProvider.class, method="selects")
	@Results({
		@Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
		@Result(column = "username", property = "username", jdbcType = JdbcType.VARCHAR),
		@Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR)
	})
	List<UserPo> selects(@Param("page") int page, @Param("rows") int rows, @Param("order") String order, @Param("query") String query);

	@SelectProvider(type = UserMapperProvider.class, method="count")
	@Results({ @Result(column="num", jdbcType = JdbcType.INTEGER) })
	int count(@Param("query") String query);

}